<!--
 * @Author: anxia.ack anchenkai@come-future.com
 * @Date: 2025-04-20 00:13:16
 * @LastEditors: anxia.ack anchenkai@come-future.com
 * @LastEditTime: 2025-04-21 21:57:58
 * @FilePath: /free-learning/src/views/course-manage/course-review/index.vue
 * @Description: 课程评论管理页面
-->
<script setup lang="ts">
import { rateTexts } from "@/constants/rateTexts";
import { quertReviewPage } from "@/api/review/course-review";
import { Search, Refresh } from "@element-plus/icons-vue";
import { getCourseListByConsultantId } from "@/api/course/course-mange";
import type { CourseInfo } from "@/api/course/course-mange/type";
import { useUserStore } from "@/store";
import { CourseReview } from "@/api/review/course-review/type";

defineOptions({
  name: "CourseReview",
});

const userStore = useUserStore();

// 查询条件
const queryForm = ref({
  pageNum: 1,
  pageSize: 10,
  keyword: undefined,
  courseId: undefined,
  consultantId:
    userStore.userInfo.roles.includes("ADMIN") || userStore.userInfo.roles.includes("ROOT")
      ? undefined
      : userStore.userInfo.userId,
});

// 表格数据
const tableData = ref<CourseReview[]>([]);
const total = ref(0);
const loading = ref(false);

// 课程列表
const courseList = ref<CourseInfo[]>([]);
// 咨询师列表

// 获取课程列表
const queryCourseList = async () => {
  const res = await getCourseListByConsultantId({
    consultantId: queryForm.value.consultantId,
  });
  courseList.value = res;
};

// 获取数据
const fetchData = async () => {
  try {
    loading.value = true;
    const res = await quertReviewPage(queryForm.value);
    tableData.value = res.records;
    total.value = res.total;
  } catch (error) {
    ElMessage.error("获取数据失败");
  } finally {
    loading.value = false;
  }
};

// 处理页码变化
const handleCurrentChange = (page: number) => {
  queryForm.value.pageNum = page;
  fetchData();
};

// 处理每页条数变化
const handleSizeChange = (size: number) => {
  queryForm.value.pageSize = size;
  queryForm.value.pageNum = 1;
  fetchData();
};

// 处理查询
const handleQuery = () => {
  queryForm.value.pageNum = 1;
  fetchData();
};

// 重置查询
const resetQuery = () => {
  queryForm.value = {
    pageNum: 1,
    pageSize: 10,
    keyword: undefined,
    courseId: undefined,
    consultantId: userStore.userInfo.userId,
  };
  fetchData();
};

// 监听咨询师变化
watch(
  () => queryForm.value.consultantId,
  () => {
    queryForm.value.courseId = undefined;
    queryCourseList();
  }
);

onMounted(() => {
  fetchData();
  queryCourseList();
  if (userStore.userInfo.roles.includes("ADMIN") || userStore.userInfo.roles.includes("ROOT")) {
  }
});
</script>

<template>
  <div class="app-container">
    <!-- 搜索工具栏 -->
    <el-card class="mb-4 !border-none" shadow="never">
      <el-form ref="queryFormRef" :model="queryForm" inline>
        <el-form-item label="关键字" prop="keyword">
          <el-input
            v-model="queryForm.keyword"
            placeholder="请输入关键字"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="课程" prop="courseId">
          <el-select
            v-model="queryForm.courseId"
            placeholder="请选择课程"
            clearable
            style="width: 200px"
          >
            <el-option
              v-for="item in courseList"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleQuery">搜索</el-button>
          <el-button :icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 数据表格 -->
    <el-card class="!border-none" shadow="never">
      <el-table v-loading="loading" :data="tableData" border>
        <el-table-column type="index" label="序号" width="60" align="center" />
        <el-table-column prop="courseName" label="课程名称" min-width="200" show-overflow-tooltip />
        <el-table-column prop="nickName" label="评论用户" width="120" align="center" />
        <el-table-column prop="comment" label="评论内容" min-width="300" show-overflow-tooltip />
        <el-table-column prop="rating" label="评分" width="160" align="center">
          <template #default="{ row }">
            <el-rate v-model="row.rating" disabled text-color="#ff9900" />
          </template>
        </el-table-column>
        <el-table-column prop="reviewTime" label="评论时间" width="180" align="center" />
      </el-table>

      <!-- 分页 -->
      <div class="mt-4 flex justify-end">
        <el-pagination
          v-model:current-page="queryForm.pageNum"
          v-model:page-size="queryForm.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<style lang="scss" scoped>
.app-container {
  padding: 20px;

  :deep(.el-card__body) {
    padding-bottom: 0;
  }
}
</style>
